package apple.encryptedNote.activities;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import apple.encryptedNode.R;
import apple.encryptedNode.dao.OperationDAO;

public class NewNoteActivity extends Activity {
	private OperationDAO operationDAO;
	private EditText titleEditText;
	private EditText contentEditText;
	private Button saveButton;
	private Button cancelButton;
	private int keyId;
	private String keyName;
	private String keyContent;
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.newnote);
		keyId = getIntent().getIntExtra("keyId", -1);
		keyName = getIntent().getStringExtra("keyName");
		keyContent = getIntent().getStringExtra("keyContent");
		operationDAO = new OperationDAO(this);
		
		//Intent intent = getIntent();
		titleEditText = (EditText)findViewById(R.id.title);
		contentEditText = (EditText)findViewById(R.id.content);
		
		if(keyName != null || keyContent!= null){
			titleEditText.setText(keyName);
			contentEditText.setText(keyContent);
		}
		titleEditText.performClick();
		titleEditText.requestFocus();
		//titleEditText.setPressed(true);
		titleEditText.setSelected(true);
		saveButton = (Button)findViewById(R.id.saveButton);
		cancelButton = (Button)findViewById(R.id.cancelButton);
		performButtonAction();
		
	}
	public void performButtonAction(){
		saveButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				String title = titleEditText.getText().toString();
				String content = contentEditText.getText().toString();
				if(keyId != -1){
					operationDAO.update(keyId, title, content);
				}else{
					operationDAO.save(title, content);
				}
				
				Toast.makeText(NewNoteActivity.this, "Save Successfully!", Toast.LENGTH_SHORT).show();
			}
		});
		cancelButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				NewNoteActivity.this.finish();
				
			}
		});
	}
	
}
